package com.dayu.finecomm.mongo.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import com.dayu.finecomm.mongo.pojo.IotDevice;
import com.dayu.finecomm.mongo.pojo.IotPlat;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

/**
 * IotPlat数据的查询和插入操作
 * 
 * @author XuJialiang
 * @since 2017-09-11 14:45:12
 * @version V1.0.0
 */
@Service
public class IotPlatService implements InitializingBean {
	@Resource
	MongoDAO dao;

	/**
	 * 插入
	 * 
	 * @param iotplat
	 */
	public void insert(IotPlat iotplat) {
		dao.nbiot.insert(iotplat);
	}

	/**
	 * 更新
	 * 
	 * @param appid
	 * @param iotplat
	 */
	public void update(String appid, IotPlat iotplat) {
		dao.nbiot.update(new BasicDBObject("appid", appid), iotplat);
	}

	/**
	 * 删除
	 * 
	 * @param appid
	 */
	public void delete(String appid) {
		dao.nbiot.remove(new BasicDBObject("appid", appid));
	}

	/**
	 * 查询
	 * 
	 * @param appid
	 * @return
	 */
	public IotPlat query(String appid) {
		IotPlat iotplat = null;

		try {
			DBCursor cursor = dao.nbiot.find(new BasicDBObject("appid", appid));

			List<DBObject> dbArray = cursor.toArray();
			iotplat = new IotPlat();
			iotplat.setAppId(dbArray.get(0).get("appid").toString());
			iotplat.setSecret(dbArray.get(0).get("secret").toString());
			iotplat.setService(dbArray.get(0).get("service").toString());
			iotplat.setExpiresIn(Integer.parseInt(dbArray.get(0)
					.get("expiresIn").toString()));
			iotplat.setAccessToken(dbArray.get(0).get("accessToken").toString());
			iotplat.setRefreshToken(dbArray.get(0).get("refreshToken")
					.toString());
			iotplat.setProtocol(dbArray.get(0).get("protocol").toString());
			iotplat.setRefreshTime(Long.parseLong(dbArray.get(0)
					.get("refreshTime").toString()));
			iotplat.setSubscribeCmds(Integer.parseInt(dbArray.get(0)
					.get("subscribeCmds").toString()));
			iotplat.setSubscribeDatas(Integer.parseInt(dbArray.get(0)
					.get("subscribeDatas").toString()));
		} catch (Exception e) {
			iotplat = null;
		}

		return iotplat;
	}

	/**
	 * 查询所有App
	 * 
	 * @return
	 */
	public List<IotPlat> queryAll() {
		List<IotPlat> list = null;

		try {
			DBCursor cursor = dao.nbiot.find();
			List<DBObject> dbArray = cursor.toArray();

			list = new ArrayList<IotPlat>(dbArray.size());

			for (int i = 0; i < dbArray.size(); i++) {
				IotPlat iotplat = new IotPlat();
				iotplat.setAppId(dbArray.get(i).get("appid").toString());
				iotplat.setSecret(dbArray.get(i).get("secret").toString());
				iotplat.setService(dbArray.get(i).get("service").toString());
				iotplat.setExpiresIn(Integer.parseInt(dbArray.get(i)
						.get("expiresIn").toString()));
				iotplat.setAccessToken(dbArray.get(i).get("accessToken")
						.toString());
				iotplat.setRefreshToken(dbArray.get(i).get("refreshToken")
						.toString());
				iotplat.setProtocol(dbArray.get(i).get("protocol").toString());
				iotplat.setRefreshTime(Long.parseLong(dbArray.get(i)
						.get("refreshTime").toString()));
				iotplat.setSubscribeCmds(Integer.parseInt(dbArray.get(i)
						.get("subscribeCmds").toString()));
				iotplat.setSubscribeDatas(Integer.parseInt(dbArray.get(i)
						.get("subscribeDatas").toString()));

				list.add(iotplat);
			}
		} catch (Exception e) {
			list = null;
		}

		return list;
	}

	/**
	 * 以下接口为NB Device
	 */

	/**
	 * 插入
	 * 
	 * @param iotdevice
	 */
	public void insertDev(IotDevice iotdevice) {
		dao.nbdev.insert(iotdevice);
	}

	/**
	 * 更新
	 * 
	 * @param devid
	 * @param iotdevice
	 */
	public void updateDev(String devid, IotDevice iotdevice) {
		dao.nbdev.update(new BasicDBObject("devId", devid), iotdevice);
	}

	/**
	 * 删除
	 * 
	 * @param appid
	 */
	public void deleteDev(String devid) {
		dao.nbdev.remove(new BasicDBObject("devId", devid));
	}

	/**
	 * 查询
	 * 
	 * @param devid
	 * @return
	 */
	public IotDevice queryDev(String devid) {
		IotDevice iotdevice = null;

		try {
			DBCursor cursor = dao.nbdev.find(new BasicDBObject("devId", devid));

			List<DBObject> dbArray = cursor.toArray();

			iotdevice = new IotDevice();
			iotdevice.setDeviceId(dbArray.get(0).get("devId").toString());
			iotdevice.setAppId(dbArray.get(0).get("appId").toString());
			iotdevice
					.setVerifyCode(dbArray.get(0).get("verifyCode").toString());
			iotdevice.setTimeout(Integer.parseInt(dbArray.get(0).get("timeout")
					.toString()));
			iotdevice.setPsk(dbArray.get(0).get("psk").toString());
			iotdevice
					.setDeviceType(dbArray.get(0).get("deviceType").toString());
			iotdevice.setMfrsId(dbArray.get(0).get("mfrsId").toString());
			iotdevice.setMfrsName(dbArray.get(0).get("mfrsName").toString());
			iotdevice.setModel(dbArray.get(0).get("model").toString());
			iotdevice.setProtocolType(dbArray.get(0).get("protocolType")
					.toString());
		} catch (Exception e) {
			iotdevice = null;
		}

		return iotdevice;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
	}
}
